import httpUtil from "../utils/httpUtil"
import logApi from "./logApi"

const send = (mobile, callBack, errCallBack) => {
	let params = {
		pid: getPid(),
		mobile
	}
	httpUtil.post('/api/Clb/sendSms', params, res => {
		if (callBack) {
			callBack(res)
		}
		logApi.addClbLog('发送验证码。参数:' + JSON.stringify(params) + '。返回值：' + JSON.stringify(res))
	}, err => {
		if (errCallBack) {
			errCallBack(err)
		}
		logApi.addClbLog('发送验证码发生错误。参数:' + JSON.stringify(params) + '。返回值：' + JSON.stringify(err))
	})
}

/**
 * 登录目前 37 ~ 50 需要
 * @doc https://console-docs.apipost.cn/preview/9f5956c5af9619ed/c564d8c78565e520?target_id=240c4dfb-7aee-4a93-b282-a7c844703306
 * @param mobile
 * @param infoOrderNum
 * @param msgExtendParam
 * @param callBack
 * @param errCallBack
 */
const login = (mobile, smsCode, callBack, errCallBack) => {
	let params = {
		pid: getPid(),
		mobile,
		smsCode,
	}
	if (getInfoOrderNum()) {
		params.infoOrderNum = getInfoOrderNum()
	}
	if (getMsgExtendParam()) {
		params.msgExtendParam = getMsgExtendParam()
	}
	httpUtil.post('/api/Clb/smsLogin', params, res => {
		if (callBack) {
			callBack(res)
		}
		logApi.addClbLog('登录。参数:' + JSON.stringify(params) + '。返回值：' + JSON.stringify(res))
	}, err => {
		if (errCallBack) {
			errCallBack(err)
		}
		logApi.addClbLog('登录发生错误。参数:' + JSON.stringify(params) + '。返回值：' + JSON.stringify(err))
	})
}

const setPid = (pid) => {
	uni.setStorageSync('pid', pid)
}

const getPid = () => {
	return uni.getStorageSync('pid')
}

const setMsgExtendParam = (msgExtendParam) => {
	if (typeof(msgExtendParam) != "string") {
		msgExtendParam = JSON.stringify(msgExtendParam)
	}
	uni.setStorageSync('msgExtendParam', msgExtendParam)
}

const getMsgExtendParam = () => {
	return uni.getStorageSync('msgExtendParam')
}

const setInfoOrderNum = (infoOrderNum) => {
	uni.setStorageSync('infoOrderNum', infoOrderNum)
}

const getInfoOrderNum = (infoOrderNum) => {
	return uni.getStorageSync('infoOrderNum')
}

/**
 * 提交订单信息
 * @param mobile
 * @param smsCode
 * @param infoOrderNum
 * @param backMethod
 * @returns {*}
 */
const submitOrder = (mobile, smsCode, backMethod, errCallBack) => {
	let params = {
		pid: getPid(),
		mobile,
		smsCode,
		a: httpUtil.getA()
	}
	if (getMsgExtendParam()) {
		params.msgExtendParam = getMsgExtendParam()
	}
	if (getInfoOrderNum()) {
		params.infoOrderNum = getInfoOrderNum()
	}
	if (getParam()) {
		params.param = getParam()
	}
	if (getOpenId()) {
		params.openid_xcx = getOpenId()
	}
	return httpUtil.post("/api/Clb/placeOrder", params, res => {
		if (backMethod) {
			backMethod(res)
		}
		logApi.addClbLog('提交订单。参数:' + JSON.stringify(params) + '。返回值：' + JSON.stringify(res))
	}, errRes => {
		if (errCallBack) {
			errCallBack(errRes)
		}
		logApi.addClbLog('提交订单发生错误。参数:' + JSON.stringify(params) + '。返回值：' + JSON.stringify(errRes))
	})
}

const setParam = (param) => {
	uni.setStorageSync('param', param)
}

const getParam = () => {
	return uni.getStorageSync('param')
}

const setOpenId = (openId) => {
	uni.setStorageSync('openid', openId)
}

const getOpenId = () => {
	return uni.getStorageSync('openid')
}

export default {
	send,
	login,
	setPid,
	getPid,
	setMsgExtendParam,
	getMsgExtendParam,
	setInfoOrderNum,
	getInfoOrderNum,
	setParam,
	getParam,
	submitOrder,
	setOpenId,
	getOpenId
}